package ai.people.platform.jx.controller;


import ai.people.netmon.framework.domain.common.SimpleNameValueRespVO;
import ai.people.netmon.framework.domain.jx.AccountPosition;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.jx.service.IAccountPositionService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 *  账号地理位置相关接口
 * @author LiGuanNan
 * @date 2022/5/31 下午2:23
 */
@Slf4j
@RestController
@RequestMapping("v1/account-position")
public class AccountPositionController {

    @Autowired
    private IAccountPositionService accountPositionService;


    /**
     * 城市列表
     */
    @GetMapping(value = "cities")
    public Result<List<SimpleNameValueRespVO>> cities() {
        return ResultUtil.success(accountPositionService.cities());
    }

    /**
     * 账号位置列表
     */
    @GetMapping(value = "points")
    public Result<List<AccountPosition>> points(@RequestParam String city) {
        return ResultUtil.success(accountPositionService.accountPoints(city));
    }

    /**
     * 清除指定城市账号经纬度缓存
     */
    @GetMapping(value = "clear-cache")
    public Result<Integer> clearCache(@RequestParam String city) {
        accountPositionService.clearCache(city);
        return ResultUtil.success();
    }

    @PostMapping("/json")
    public JSONArray upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        JSONArray jsonArray = new JSONArray();
        String s = new String(multipartFile.getBytes());
        JSONObject parseObject = JSONObject.parseObject(s);
        JSONObject hits1 = parseObject.getJSONObject("hits");
        JSONArray hits = hits1.getJSONArray("hits");
        hits.forEach(o->{
            JSONObject jsonObject = (JSONObject) o;
            JSONObject source = jsonObject.getJSONObject("_source");
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("_id", jsonObject.get("_id"));
            jsonObject1.putAll(source);
            jsonArray.add(jsonObject1);
        });
        return jsonArray;
    }

}
